﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.MissileArrive
{
    public partial class MissileArriveInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserPermissionBll userPermissionBll = new UserPermissionBll();
                if (!userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.MissileArrive))
                {
                    Response.Write("您没有权限使用该功能");
                    Response.End();
                    return;
                }
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string itemCode = txtItemCode.Text.Trim();
            string distNumber = txtDistNumber.Text.Trim();
            string inDateFrom = txtInDateFrom.Text.Trim();
            string inDateTo = txtInDateTo.Text.Trim();

            string sql = @"SELECT T0.ItemCode,T3.ItemName,DistNumber,T2.SysNumber,T2.InDate 
                            ,T2.u_yccsj ,T2.u_tdsj ,t2.u_gfsj ,t2.u_cchj ,t2.u_yfsj, t2.u_qfsj ,t2.U_LifeTime ,t2.u_level, t2.u_FlightTime
                            FROM OSRQ T0
                            JOIN OWHS T1 ON T1.WhsCode=T0.WhsCode
                            JOIN OSRN T2 ON T2.SysNumber=T0.SysNumber AND T2.ItemCode = T0.ItemCode
                            JOIN OITM T3 ON T3.ItemCode=T0.ItemCode
                            WHERE T1.U_Class1=103 AND T1.U_Class2=202 AND T0.Quantity>0 {0} ORDER BY T2.InDate DESC";

            string where = string.Empty;

            if (!string.IsNullOrWhiteSpace(itemCode))
            {
                where += "AND T0.ItemCode LIKE '%" + itemCode + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(distNumber))
            {
                where += "AND DistNumber LIKE '%" + distNumber + "%' ";
            }
            if (!string.IsNullOrWhiteSpace(inDateFrom))
            {
                where += "AND InDate >= '" + inDateFrom + " 00:00:00' ";
            }
            if (!string.IsNullOrWhiteSpace(inDateTo))
            {
                where += "AND InDate <= '" + inDateTo + " 23:23:59'";
            }

            sql = string.Format(sql, where);

            Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string itemCode = gridView.DataKeys[gridViewRow.RowIndex]["ItemCode"].ToString();
            string sysnumber = gridView.DataKeys[gridViewRow.RowIndex]["SysNumber"].ToString();

            switch (e.CommandName)
            {
                case "CmdEdit":
                    Response.Redirect(string.Format("MissileArriveEdit.aspx?itemCode={0}&sysnumber={1}", itemCode, sysnumber));
                    break;
                default:
                    break;
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            string strInDate = gridView.DataKeys[e.Row.RowIndex]["InDate"].ToString();
            string strLifeTime = gridView.DataKeys[e.Row.RowIndex]["U_LifeTime"].ToString();

            DateTime inDate = DateTime.Now;
            if (!DateTime.TryParse(strInDate, out inDate))
            {
                return;
            }

            int lifeTime = 0;
            if (!int.TryParse(strLifeTime, out lifeTime))
            {
                return;
            }

            TimeSpan timeSpan = DateTime.Now.Subtract(inDate);
            double temp = timeSpan.Days / 30.00;
            double newLifeTime = lifeTime - temp;
            if (newLifeTime < 0)
            {
                newLifeTime = 0;
            }

            e.Row.Cells[11].Text = Math.Round(newLifeTime,2).ToString();
        }
    }
}